package com.yanqu.road.dao.impl.activity.pawnshop;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.pawnshop.PawnshopMoney;
import com.yanqu.road.entity.activity.pawnshop.PawnshopNpc;
import com.yanqu.road.entity.activity.pawnshop.UserPawnshopEvent;

public class UserPawnshopEventDaoImpl extends TempDataObjectDao<UserPawnshopEvent> {

	public static String getUserPawnshopEventSql = "select * from `t_u_activity_pawnshop_event` where `userId`=? and `activityId`=? and finish = 0";
	public UserPawnshopEvent getUserPawnshopEvent(long userId,int activityId) {
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(getUserPawnshopEventSql);
			int i = 1;
			call.setLong(i++, userId);
			call.setInt(i++, activityId);
			rs = call.executeQuery();
			if (rs.next()) {
				return parse(rs);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public static String updateUserPawnshopEventSql = "update `t_u_activity_pawnshop_event` set type=?,npc=?,itemList=?,money=?,isFake=?,washTime=?,userBid=?,status=?,finish=?,patient=?,priceList=?,npcBid=?,npcWantPrice=?,valuationPrice=?,maxPrice=? where userId=? and activityId=? and eventId=?";
	@Override
	protected boolean updateData(UserPawnshopEvent userPawnshopEvent) {
		userPawnshopEvent.setBeginWriteTime();
		getDb().setConnName(DbNameStrategy.getUserDb());
		int i = 1;
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopEvent.getType()));
		para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userPawnshopEvent.getNpc())));
		para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userPawnshopEvent.getItemList())));
		para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userPawnshopEvent.getMoney())));
		para.put(i++, new DbParameter(Types.BOOLEAN, userPawnshopEvent.getIsFake()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopEvent.getWashTime()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopEvent.getUserBid()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopEvent.getStatus()));
		para.put(i++, new DbParameter(Types.BOOLEAN, userPawnshopEvent.isFinish()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopEvent.getPatient()));
		para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userPawnshopEvent.getPriceList())));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopEvent.getNpcBid()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopEvent.getNpcWantPrice()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopEvent.getValuationPrice()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopEvent.getMaxPrice()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopEvent.getUserId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopEvent.getActivityId()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopEvent.getEventId()));
		if (getDb().executeNoneQuery(updateUserPawnshopEventSql, para) > 0) {
			return true;
		}
		return false;
	}

	public static String insertUserPawnshopEventSql = "insert into `t_u_activity_pawnshop_event`(userId,activityId,eventId,type,npc,itemList,money,isFake,washTime,userBid,status,finish,patient,priceList,npcBid,npcWantPrice,valuationPrice,maxPrice) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
	@Override
	protected boolean addData(UserPawnshopEvent userPawnshopEvent) {
		userPawnshopEvent.setBeginWriteTime();
		getDb().setConnName(DbNameStrategy.getUserDb());
		int i = 1;
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopEvent.getUserId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopEvent.getActivityId()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopEvent.getEventId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopEvent.getType()));
		para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userPawnshopEvent.getNpc())));
		para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userPawnshopEvent.getItemList())));
		para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userPawnshopEvent.getMoney())));
		para.put(i++, new DbParameter(Types.BOOLEAN, userPawnshopEvent.getIsFake()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopEvent.getWashTime()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopEvent.getUserBid()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopEvent.getStatus()));
		para.put(i++, new DbParameter(Types.BOOLEAN, userPawnshopEvent.isFinish()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopEvent.getPatient()));
		para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userPawnshopEvent.getPriceList())));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopEvent.getNpcBid()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopEvent.getNpcWantPrice()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopEvent.getValuationPrice()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopEvent.getMaxPrice()));
		if (getDb().executeNoneQuery(insertUserPawnshopEventSql, para) > 0) {
			return true;
		}
		return false;

	}

	@Override
	protected UserPawnshopEvent parseData(ResultSet rs) throws SQLException {
		UserPawnshopEvent userPawnshopEvent = new UserPawnshopEvent();
		userPawnshopEvent.setUserId(rs.getLong("userId"));
		userPawnshopEvent.setActivityId(rs.getInt("activityId"));
		userPawnshopEvent.setEventId(rs.getLong("eventId"));
		userPawnshopEvent.setType(rs.getInt("type"));
		userPawnshopEvent.setNpc(JSON.parseObject(rs.getString("npc"), PawnshopNpc.class));
		userPawnshopEvent.setItemList(JSON.parseObject(rs.getString("itemList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.pawnshop.PawnshopItem>>() {}));
		userPawnshopEvent.setMoney(JSON.parseObject(rs.getString("money"), PawnshopMoney.class));
		userPawnshopEvent.setIsFake(rs.getBoolean("isFake"));
		userPawnshopEvent.setWashTime(rs.getInt("washTime"));
		userPawnshopEvent.setUserBid(rs.getLong("userBid"));
		userPawnshopEvent.setStatus(rs.getInt("status"));
		userPawnshopEvent.setFinish(rs.getBoolean("finish"));
		userPawnshopEvent.setPatient(rs.getInt("patient"));
		userPawnshopEvent.setPriceList(JSON.parseObject(rs.getString("priceList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.pawnshop.PawnshopBidPrice>>() {}));
		userPawnshopEvent.setNpcBid(rs.getLong("npcBid"));
		userPawnshopEvent.setNpcWantPrice(rs.getLong("npcWantPrice"));
		userPawnshopEvent.setValuationPrice(rs.getLong("valuationPrice"));
		userPawnshopEvent.setMaxPrice(rs.getLong("maxPrice"));

		return userPawnshopEvent;
	}


}
